package com.basic.dynamic.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class PersonDynamic implements InvocationHandler{

	private Persion persion;
	
	public PersonDynamic(Persion persion) {
		this.persion = persion;
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		befor();
		return method.invoke(persion, args);
	}
	
	public Object createProxy(){
		return Proxy.newProxyInstance(persion.getClass().getClassLoader(), persion.getClass().getInterfaces(), this);
	}

	public void befor(){
		System.out.println("给地面洒水.........");
	}
	
	public static void main(String[] args) {
		Student student = new Student();
		PersonDynamic dynamic = new PersonDynamic(student);
		Persion persion = (Persion) dynamic.createProxy();
		persion.clean();
	}
}
